package com.yiwei.hdb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.base.ItemAdapter;
import com.yiwei.hdb.model.DealUseModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class UsePurposeAdapter extends ItemAdapter<DealUseModel.DataBean, UsePurposeAdapter.DefaultHolder> {


    public UsePurposeAdapter(Context context) {
        super(context);
    }

    private boolean[] isCheck;

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_use_purpose, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        DealUseModel.DataBean item = getItem(position);
        holder.txtContent.setText(item.name);
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.id);
                }
                for (int i = 0; i < isCheck.length; i++) {
                    if (i == position) {
                        isCheck[i] = true;
                    } else {
                        isCheck[i] = false;
                    }
                    notifyDataSetChanged();
                }
            }
        });
        if (isCheck[position]) {
            holder.cbCheck.setImageResource(R.mipmap.img_use_deal_true);
        } else {
            holder.cbCheck.setImageResource(R.drawable.button_user_purpose_false);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String content);
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_content)
        TextView txtContent;
        @BindView(R.id.cb_check)
        ImageView cbCheck;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public void update(List<DealUseModel.DataBean> strings) {
        isCheck = new boolean[strings.size()];
        super.update(strings);
    }
}
